//
//  LJLHistoryVC.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "LJLHistoryVC.h"
#import "LJLRecommendedTVC.h"
#import "LJLRecommendedModel.h"
#import "LTDataBase.h"
#import "LTHistorySoundTableViewCell.h"
#import "LTHistroyRadioTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"
#import "LTHistoryHeaderTableViewCell.h"
#import "DownSheet.h"
#import "ZCSListenListListModel.h"
#import "ZCSPlayMusicViewController.h"

typedef void(^arrayB)(NSMutableArray *);

@interface LJLHistoryVC ()<UITableViewDelegate, UITableViewDataSource, soundPro, radioPro, DownSheetDelegate>

@property(nonatomic, strong) UITableView *historyTV;

@property(nonatomic, strong) NSMutableArray *soundArray;

@property (nonatomic, strong) NSMutableArray *radioArray;

@property (nonatomic, assign) BOOL isEdit;

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) NSArray *sheetArray;

@property (nonatomic, retain) LTVoiceModel *voiceModel;
@property (nonatomic, retain) LJLRadioPageModel *radioModel;
@property (nonatomic, retain) NSMutableArray *playArray;

@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) NSInteger model_id;

@property (nonatomic, copy) arrayB aBlock;
@end

@implementation LJLHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self createView];
    [self initSheetView];
}
- (void)initSheetView
{
    DownSheetModel *Model_1 = [[DownSheetModel alloc]init];
    Model_1.icon = @"deleta";
    Model_1.icon_on = @"deletaH";
    Model_1.title = @"取消收藏";
    DownSheetModel *Model_2 = [[DownSheetModel alloc]init];
    Model_2.icon = @"关闭zcs";
    Model_2.icon_on = @"关闭zcsh";
    Model_2.title = @"关闭";
    self.sheetArray = @[Model_1, Model_2];
}
- (void)createView
{
    self.historyTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREENWIDTH, SCREENHEIGHT - 104) style:UITableViewStyleGrouped];
    self.historyTV.delegate = self;
    self.historyTV.dataSource = self;
    [self.view addSubview:_historyTV];
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50 * FITHEIGHT)];
    self.historyTV.tableFooterView = footView;
    
    [self.historyTV registerClass:[LJLRecommendedTVC class] forCellReuseIdentifier:@"LJLhistoryTVC"];
    [self.historyTV registerNib:[UINib nibWithNibName:@"LTHistorySoundTableViewCell" bundle:nil] forCellReuseIdentifier:@"sound"];
    [self.historyTV registerNib:[UINib nibWithNibName:@"LTHistroyRadioTableViewCell" bundle:nil] forCellReuseIdentifier:@"radio"];
    [self.historyTV registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
    [self.historyTV registerNib:[UINib nibWithNibName:@"LTHistoryHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"historyHeader"];
    self.historyTV.separatorStyle = 0;
    [self tableHeaderView];
    
    self.backImageView = [[UIImageView alloc] initWithFrame:self.historyTV.bounds];
    self.backImageView.image = [UIImage imageNamed:@"收听"];
    self.backImageView.hidden = YES;
    [self.view addSubview:_backImageView];
    
    
}




- (void)tableHeaderView
{
    LTHistoryHeaderTableViewCell *header = [self.historyTV dequeueReusableCellWithIdentifier:@"historyHeader"];
    __weak LJLHistoryVC *weak = self;
    
    header.aBlock = ^(void)
    {
        LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        [data deleteAllCourseWith:4];
        [data deleteAllCourseWith:2];
        [weak getData];
    };
    header.frame = CGRectMake(0, 0, SCREENWIDTH, 40 * FITHEIGHT);
    [self.view addSubview:header];
   
}

- (void)getData
{
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
   self.soundArray = [data selectAllWithtype:4];
    self.radioArray = [data selectAllWithtype:2];
    [self.historyTV reloadData];
}

- (void)pushRadio:(LJLRadioPageModel *)model
{
    self.radioModel = model;
    self.type = 2;
    self.model_id = model.radioId;
    [self showAlretView];
}

- (void)pushList:(LTVoiceModel *)model
{
    
    self.voiceModel = model;
    self.type = 4;
    self.model_id = model.trackId;
    [self showAlretView];
}

- (void)showAlretView
{
    DownSheet *sheet = [[DownSheet alloc]initWithlist:self.sheetArray height:10];
    
    sheet.delegate = self;
    [sheet showInView:nil];
}
- (void)didSelectIndex:(NSInteger)index{
    if (index == 0) {
        LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        [data delegateDataWithModel_id:self.model_id type:self.type];
        [self getData];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    __weak LJLHistoryVC *hisVC = self;
    if (self.soundArray.count != 0 && self.radioArray.count != 0) {
        if (indexPath.section == 0) {
            LTVoiceModel *model = [self.soundArray objectAtIndex:indexPath.row];
            [self getMoreDataWith:model.albumId page:1];
            self.aBlock = ^(NSMutableArray *playDataSource)
            {

                ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
                play.musicIndex = model.musicIndex;
                play.dataArray = playDataSource;
                play.maxPage = model.maxPage;
                [hisVC presentViewController:play animated:YES completion:nil];
                
            };
            return;
        }
        else
        {
            LJLRadioPageModel *model = [self.radioArray objectAtIndex:indexPath.row];
            ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
            NSMutableArray *tempArray = [NSMutableArray array];
            [tempArray addObject:model];
            play.dataArray = tempArray;
            play.musicIndex = 0;
            play.isRadio = YES;
            [self presentViewController:play animated:YES completion:nil];
            return;
        }
    }
    if (self.soundArray.count != 0 && self.radioArray.count == 0) {
        LTVoiceModel *model = [self.soundArray objectAtIndex:indexPath.row];
        [self getMoreDataWith:model.albumId page:1];
        self.aBlock = ^(NSMutableArray *playDataSource)
        {
            
            ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
            play.musicIndex = model.musicIndex;
            play.dataArray = playDataSource;
            play.maxPage = model.maxPage;
            [hisVC presentViewController:play animated:YES completion:nil];
            
        };
        return;
    }
    LJLRadioPageModel *model = [self.radioArray objectAtIndex:indexPath.row];
    ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
    NSMutableArray *tempArray = [NSMutableArray array];
    [tempArray addObject:model];
    play.dataArray = tempArray;
    play.musicIndex = 0;
    play.isRadio = YES;
    [self presentViewController:play animated:YES completion:nil];
    
    
}


- (void)getMoreDataWith:(NSInteger)albumId page:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/v1/album/track?albumId=%ld&device=iPhone&isAsc=true&pageId=%ld&pageSize=20", albumId, page];
    __weak LJLHistoryVC *pmvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        pmvc.playArray = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"][@"list"]) {
            ZCSListenListListModel *model = [[ZCSListenListListModel alloc] initWithDic:dic];
            [pmvc.playArray addObject:model];
        }
        pmvc.aBlock(_playArray);

    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [self getData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.soundArray.count != 0 && self.radioArray.count != 0) {
        if (section == 0) {
            ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
            cell.headerLabel.text = @"您播放的声音";
            cell.backgroundColor = [UIColor whiteColor];
            cell.headerLabel.textColor = [UIColor blackColor];
            cell.moreImage.hidden = YES;
            cell.moreLabel.hidden = YES;
            return cell;
        }
        ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
        cell.headerLabel.text = @"您播放的广播";
        cell.backgroundColor = [UIColor whiteColor];
        cell.headerLabel.textColor = [UIColor blackColor];
        cell.moreImage.hidden = YES;
        cell.moreLabel.hidden = YES;
        return cell;
        
    }
    if (self.soundArray.count != 0 && self.radioArray.count == 0) {
        ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
        cell.headerLabel.text = @"您播放的声音";
        cell.backgroundColor = [UIColor whiteColor];
        cell.headerLabel.textColor = [UIColor blackColor];
        cell.moreImage.hidden = YES;
        cell.moreLabel.hidden = YES;
        return cell;
    }
    
    ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    cell.headerLabel.text = @"您播放的广播";
    cell.backgroundColor = [UIColor whiteColor];
    cell.headerLabel.textColor = [UIColor blackColor];
    cell.moreImage.hidden = YES;
    cell.moreLabel.hidden = YES;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if (self.soundArray.count == 0 && self.radioArray.count == 0) {
        self.backImageView.hidden = NO;
        return 0;
    }
    if (self.soundArray.count != 0 && self.radioArray.count != 0) {
        self.backImageView.hidden = YES;
        return 2;
    }
    self.backImageView.hidden = YES;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.soundArray.count != 0 && self.radioArray.count != 0) {
        if (section == 0) {
            return self.soundArray.count;
        }
        return self.radioArray.count;
    }
    if (self.soundArray.count != 0 && self.radioArray.count == 0) {
        return self.soundArray.count;
    }
    return self.radioArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.soundArray.count != 0 && self.radioArray.count != 0) {
        if (indexPath.section == 0) {
            LTHistorySoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sound"];
            cell.delegata = self;
            cell.model = [self.soundArray objectAtIndex:indexPath.row];
            return cell;
        }
        LTHistroyRadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radio"];
        cell.model = [self.radioArray objectAtIndex:indexPath.row];
        cell.delegate = self;
        return cell;
        
    }
    if (self.soundArray.count != 0 && self.radioArray.count == 0) {
        LTHistorySoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sound"];
        cell.model = [self.soundArray objectAtIndex:indexPath.row];
        cell.delegata = self;
        return cell;
    }
    LTHistroyRadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radio"];
    cell.model = [self.radioArray objectAtIndex:indexPath.row];
    cell.delegate = self;
    return cell;
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
